package com.woniuxy.usercoachassemble.conversion;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;


public class DateConversion implements Converter<String, Date>{
	@Override
	public Date convert(String source) {
		if(source==null||"".equals(source)) {
			return null;
		}else {
			String pattern1="[0-9]{4}-[0-9]{2}-[0-9]{2}";
			String pattern2="[0-9]{4}/[0-9]{2}/[0-9]{2}";
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd");
			try {
				if(source.matches(pattern1)) {
					return format1.parse(source);
				}else if(source.matches(pattern2)) {
					return format2.parse(source);
				}else {
					throw new RuntimeException();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return null;
	}
}
